<?php

namespace catchAdmin\customs\model;


use catchAdmin\externalGoods\model\GoodsAttribute;
use catchAdmin\externalGoods\model\GoodsRule as externalGoodsRuleModel;
use catcher\base\CatchModel;
use catcher\CatchAuth;


class HnOrder extends CatchModel
{

    protected $name = 'db_customs_hn_order';
    protected $field = [
        'id',
        'order_id',
        'order_no',
        'push_status',
        'username',
        'user_phone',
        'order_goods',
        'is_pay',
        'id_card',
        'id_card_front_img',
        'id_card_back_img',
        'declarer',
        'declarer_id_card',
        'source_platform_no',
        'amount_freight',
        'amount_discount',
        'pay_type',
        'pay_no',
        'create_time',
        'pay_time',
        'area_id',
        'user_address',
        'remark',
        'created_at',
        'updated_at',
        'deleted_at',
        'tenant_id',
        'creator_id',
    ];

    public function getList($tenant_id = 0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }

        return $this
            ->withoutField('updated_at,deleted_at,tenant_id,creator_id,_ext_data')
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item) {
                $item['order_goods'] = json_decode($item['order_goods'],true);
                $item['order_detail'] = (new HnOrderDetail)
                    ->withoutField('created_at,updated_at,deleted_at,tenant_id,creator_id')
                    ->where('order_id',$item['id'])->select()->toArray();
            });
    }

}